<?php

namespace App\Http\Controllers\Admin;

use App\Model\Goods;
use App\Service\CommonService;
use App\Service\ShopService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class GoodsController extends Controller
{
    const view = 'admin.goods';
    const page = 10;

    public function index(Request $request,Goods $goods){
        $like = $request->input('querystr',null);
        $status= $request->input('status',-1);
        $pam['data'] = $goods->GetGoods($like,$status,self::page);
        $pam['querystr'] = $like;
        $pam['status'] = $status;
    return view(self::view.'.goods')->with($pam);
    }


    public function ChangeStatus(Request $request,Goods $goods){
            $id = $request->input('id',null);
            $status = $request->input('status',null);

            if(in_array($status,[0,1])&&!empty($id)){
                if($status==0){
                    $data['status']=1;
                }else{
                    $data['status']=0;
                }
                $data['update_time'] = strtotime('now');
               $change =   $goods->where('id',$id)->update($data);
            if($change){
                return ApiResponse([],'修改成功',1);
            }else{
                return ApiResponse([],'修改失败',0);
                }
            }else{
                return ApiResponse([],'传递的参数无效',0);
            }
    }

    public function Details(Request $request,Goods $goods){
            $id = $request->input('id');
            $data = $goods->GetGoods(null,null,null,$id);
            $data = $data->toArray()['data'][0];
            $data['newbanner'] = json_decode($data['banner'],true);
            return view(self::view.'.details')->with($data);
    }

    public function DelGoods(Request $request,Goods $goods){
        $id = $request->input('id',null);
        $change = $goods->where('id',$id)->update(['update_time'=>strtotime('now'),'is_del'=>'-1']);
        if($change){
            return ApiResponse([],'删除成功',1);
        }else{
            return ApiResponse([],'删除失败',0);
        }
    }

    public function create()
    {
        return view(self::view.'.create');
    }

    /**
     * 获取七牛云token
     * @param Request $request
     * @param CommonService $commonService
     * @return array
     */
    public function getQiniuToken(Request $request, CommonService $commonService)
    {
        $picType = $request->picType;
        $res = $commonService->picToken($picType);
        if (!$res) return ApiResponse([], $commonService->error, 1);
        return ApiResponse($res);
    }

    public function store(Request $request, ShopService $shopService)
    {
        $re = $shopService->addGoods($request->all());
        if (!$re) return ApiResponse([], $shopService->error, 1);
        return ApiResponse();
    }

    public function edit(Request $request)
    {
        $goodsId = $request->goods_id;
        $goods = DB::table('goods')->where('id', $goodsId)->first();
        $goodsInfo = DB::table('goods_info')->where('goods_id', $goodsId)->first();
        return view(self::view . '.edit', compact('goods', 'goodsInfo'));
    }

    public function update(Request $request, ShopService $shopService)
    {
        $re = $shopService->updateGoods($request->all());
        if (!$re) return ApiResponse([], $shopService->error, 1);
        return ApiResponse();
    }

}
